package com.wardrobe.soa.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * Created by m1304 on 2018/8/2.
 */
public class JsoupTest {
        public static void main(String[] args) throws IOException {
            //获取编辑推荐页
            Element document = getHtml("https://news.dahe.cn/");
            Element main=document.getElementById("c2");
            Elements li=main.getElementsByTag("li");
            for(Element liContent:li){
                String title = liContent.text();
                String url = liContent.select("a").attr("href");
                Element html = getHtml(url);
                Element mainCon = html.getElementById("mainCon");
                String content = mainCon.html();
                System.out.println(content);
            }
        }

        private static Element getHtml(String url) throws IOException {
            Document document=Jsoup.connect(url)
                    //模拟火狐浏览器
                    .userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
                    .get();
            return document;
        }
}
